//
//  CLTabPagerViewController.m
//  Charles
//
//  Created by Leo on 2019/4/26.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLTabPagerViewController.h"
#import "CLTabPagerBar.h"
#import "CLPagerView.h"

@interface CLTabPagerViewController ()<CLTabPagerBarDelegate,CLTabPagerBarDataSource,CLPagerViewDataSource,CLPagerViewDelegate>

@property (nonatomic, strong) CLTabPagerBar *tabPagerBar;

@property (nonatomic, strong) CLPagerView *pagerView;

@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation CLTabPagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *datas = [NSMutableArray array];
    for (NSInteger i = 0; i < 20; ++i) {
        [datas addObject:i%2 == 0 ? [NSString stringWithFormat:@"Tab %ld",i]:[NSString stringWithFormat:@"Tab Tab %ld",i]];
    }
    
    self.titleArray = [NSArray arrayWithArray:datas];

    self.titleArray = @[@"体育世界",@"关注",@"头条",@"新时代",@"视频",@"轻松",@"娱乐",@"问了你好",@"西游",@"独家",@"健康",@"航空"];
    
    self.tabPagerBar.backgroundColor = [UIColor whiteColor];
    [self.tabPagerBar registerClass:[CLTabPagerBarCell class] forCellWithReuseIdentifier:[CLTabPagerBarCell cellIdentifier]];
    [self.view addSubview:self.tabPagerBar];
    [self.tabPagerBar reloadData];
    
    [self.view addSubview:self.pagerView];
    [self.pagerView reloadData];
}

#pragma mark - CLTabPagerBarDelegate

- (CGFloat)pagerTabBar:(CLTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title =  [self.titleArray objectAtIndex:index];
    return [pagerTabBar cellWidthForTitle:title];
}

- (void)pagerTabBar:(CLTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
//    [_pagerView scrollToItemFromIndex:pagerTabBar.curIndex toIndex:index animated:YES];
    [_pagerView scrollToViewAtIndex:index animate:YES];
}

- (NSInteger)numberOfItemsInPagerTabBar {
    return self.titleArray.count;
}

- (UICollectionViewCell<CLTabPagerBarCellProtocol> *)pagerTabBar:(CLTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<CLTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[CLTabPagerBarCell cellIdentifier] forInex:index];
//    cell.backgroundColor = [UIColor redColor];
    cell.titleLabel.text = [self.titleArray objectAtIndex:index];
    return cell;
}

#pragma mark - CLPagerViewDelegate

- (NSInteger)numberOfViewsInPagerView {
    return self.titleArray.count;
}

- (UIView *)pagerView:(CLPagerView *)pagerView viewForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    UIView *view = [pagerView.layout dequeueReusableItemWithReuseIdentifier:@"cellId" forIndex:index];
    view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0];
    return view;
}




- (void)pagerView:(CLPagerView *)pagerView willAppearView:(UIView *)view forIndex:(NSInteger)index {
    //NSLog(@"+++++++++willAppearViewIndex:%ld",index);
}

- (void)pagerView:(CLPagerView *)pagerView willDisappearView:(UIView *)view forIndex:(NSInteger)index {
    //NSLog(@"---------willDisappearView:%ld",index);
}

- (void)pagerView:(CLPagerView *)pagerView transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    NSLog(@"fromIndex:%ld, toIndex:%ld",fromIndex,toIndex);
    [_tabPagerBar scrollToItemFromIndex:fromIndex toIndex:toIndex animated:animated];
}

- (void)pagerView:(CLPagerView *)pagerView transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    //NSLog(@"fromIndex:%ld, toIndex:%ld progress%.3f",fromIndex,toIndex,progress);
    [_tabPagerBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}

- (void)pagerViewWillBeginScrolling:(CLPagerView *)pageView animate:(BOOL)animate {
    //NSLog(@"pagerViewWillBeginScrolling");
}

- (void)pagerViewDidEndScrolling:(CLPagerView *)pageView animate:(BOOL)animate {
    //NSLog(@"pagerViewDidEndScrolling");
}


- (CLTabPagerBar *)tabPagerBar {
    if (!_tabPagerBar) {
        _tabPagerBar = [[CLTabPagerBar alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 36)];
        _tabPagerBar.delegate = self;
        _tabPagerBar.dataSource = self;
        _tabPagerBar.layout.progressHeight = 3;
        _tabPagerBar.layout.progressRadius = _tabPagerBar.layout.progressHeight / 2;
        _tabPagerBar.layout.barStyle = CLTabPagerBarStyleProgressElasticView;
        _tabPagerBar.progressView.backgroundColor = [UIColor lightGrayColor];
    }
    return _tabPagerBar;
}

- (CLPagerView *)pagerView {
    if (!_pagerView) {
        _pagerView = [[CLPagerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tabPagerBar.frame) + 20, self.view.bounds.size.width, 200)];
        _pagerView.layout.autoMemoryCache = NO;
        _pagerView.delegate = self;
        _pagerView.dataSource = self;
        [_pagerView.layout registerClass:[UIView class] forItemWithReuseIdentifier:@"cellId"];
    }
    return _pagerView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
